package com.mzy.linear_struct;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

class Person {
    String country;  
    String city;  
  
    // 构造函数、getter和setter等  
    // ...


    public Person(String country, String city) {
        this.country = country;
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Person{" +
                "country='" + country + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}
  
public class GroupingExample {  
  
    public static void main(String[] args) {  
        List<Person> people = Arrays.asList(
                new Person("USA", "New York"),  
                new Person("USA", "Los Angeles"),  
                new Person("China", "Beijing"),  
                new Person("China", "Shanghai"),  
                new Person("Japan", "Tokyo"),  
                new Person("Japan", "Osaka")  
        );  
  
        Map<String, Map<String, List<Person>>> groupedByCountryAndCity = people.stream()  
                .collect(Collectors.groupingBy(
                        Person::getCountry, // 先根据country分组  
                        Collectors.groupingBy(Person::getCity) // 在每个country分组中再根据city分组  
                ));
        System.out.println(groupedByCountryAndCity);
        // 打印分组结果  
        // for (Map.Entry<String, Map<String, List<Person>>> countryEntry : groupedByCountryAndCity.entrySet()) {
        //     System.out.println("Country: " + countryEntry.getKey());
        //     for (Map.Entry<String, List<Person>> cityEntry : countryEntry.getValue().entrySet()) {
        //         System.out.println("  City: " + cityEntry.getKey());
        //         for (Person person : cityEntry.getValue()) {
        //             System.out.println("    Person: " + person.getCity()); // 这里只是打印city作为示例，你可能想要打印整个person对象
        //         }
        //     }
        // }
    }  
}